package cn.snorlaxzjt.LBSserver.business;

import cn.snorlaxzjt.LBSserver.common.Converter;
import cn.snorlaxzjt.LBSserver.po.body.MomentSaveBody;
import cn.snorlaxzjt.LBSserver.po.body.MomentSelectBody;
import cn.snorlaxzjt.LBSserver.po.entity.Comment;
import cn.snorlaxzjt.LBSserver.po.entity.Moment;
import cn.snorlaxzjt.LBSserver.po.entity.MomentEntity;
import cn.snorlaxzjt.LBSserver.po.page.CommentPage;
import cn.snorlaxzjt.LBSserver.po.page.MomentPage;
import cn.snorlaxzjt.LBSserver.po.page.ResultPage;
import cn.snorlaxzjt.LBSserver.service.CommentService;
import cn.snorlaxzjt.LBSserver.service.MomentService;
import cn.snorlaxzjt.LBSserver.service.UserService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
public class MomentBusiness {

    @Resource
    private MomentService momentService;

    @Resource
    private UserService userService;

    @Resource
    private CommentService commentService;

    public ResultPage<List<MomentPage>> queryMyMoments(MomentSelectBody body)
    {
        body.setUserId(userService.getCurrentUserId());
        return queryMomentsByUserId(body);
    }

    public ResultPage<List<MomentPage>> queryMomentsByUserId(MomentSelectBody body)
    {
        ResultPage<List<Moment>> res = momentService.queryMomentsByUserId(body);
        return getListResultPage(res);
    }

    public ResultPage<List<MomentPage>> queryFriendsMoments( MomentSelectBody body)
    {
        body.setUserId(userService.getCurrentUserId());
        ResultPage<List<Moment>> res = momentService.queryFriendsMoments(body);
        return getListResultPage(res);
    }

    private void fillCommentPage(List<MomentPage> list){
        List<List<CommentPage>> commentList = commentService.queryByCommentIds(list.stream().map(MomentPage::getId).collect(Collectors.toList()));
        for(MomentPage moment : list){
            for(int i = 0;i < commentList.size();i++){
                if(moment.getId().equals(commentList.get(i).get(0).getMomentId())){
                    moment.setCommentList(commentList.get(i));
                    commentList.remove(i);
                    break;
                }
            }
        }
    }

    private ResultPage<List<MomentPage>> getListResultPage(ResultPage<List<Moment>> res) {
        List<Moment> list = res.getItems();
        List<MomentPage> ans = new ArrayList<>();
        for(Moment moment:list)
        {
            ans.add(Converter.toMomentPage(moment));
        }
        fillCommentPage(ans);
        ResultPage<List<MomentPage>> resultPage = new ResultPage<>();
        resultPage.setCount(res.getCount());
        resultPage.setItems(ans);
        resultPage.setCurId(res.getCurId());
        resultPage.setSize(res.getSize());
        return resultPage;
    }

    public void save(MomentSaveBody body)
    {
        MomentEntity moment = new MomentEntity();
        BeanUtils.copyProperties(body,moment);
        moment.setUserId(userService.getCurrentUserId());
        moment.setStatus(1);
        momentService.save(moment);
    }

    public void del(Integer id)
    {
        momentService.del(id);
    }

    public ResultPage<List<MomentPage>> queryMomentsInDis(BigDecimal lon, BigDecimal lat, Double distance){
        ResultPage<List<MomentPage>> resultPage = new ResultPage<>();
        List<MomentPage> list = momentService.queryMomentsInDis(lon,lat,distance).stream().map(Converter::toMomentPage).collect(Collectors.toList());
        resultPage.setItems(list);
        return resultPage;
    }

}
